package com.koron.device.service.propconfig;

import com.koron.common.core.business.standard.feign.StandardFeignService;
import com.koron.common.core.util.CommonUtils;
import com.koron.device.bean.Equipment;
import com.koron.device.bean.EquipmentQuery;
import org.apache.commons.collections4.CollectionUtils;
import org.apache.commons.lang3.StringUtils;
import org.koron.ebs.mybatis.SessionFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.util.Arrays;
import java.util.List;
import java.util.stream.Collectors;

/**
 * @author twcao
 * @title: DeviceStandardConfig
 * @projectName GDH_EAM_V2
 * @description: 设备标准配置
 * @date 2022/2/169:35
 */
@Component
public class DeviceStandardConfig implements PropConfig {

    @Autowired
    private StandardFeignService standardFeign;

    @Override
    public List<String> sign() {
        return Arrays.asList("filterStandard");
    }

    @Override
    public void setup(SessionFactory factory, List<Equipment> equipments, EquipmentQuery query) {
        if(StringUtils.isBlank(query.getStandardType())) {
            return;
        }
        List<String> typeIds = equipments.stream().map(Equipment::getTypeId).collect(Collectors.toList());
        List<String> filterTypeIds = CommonUtils.ok(standardFeign.filterStandard(typeIds, query.getStandardType()));
        // 如果没有返回分类id，则表明所有的设备分类都没有标准
        equipments.stream().forEach(equipment -> equipment.setFilterStandard(CollectionUtils.isEmpty(filterTypeIds) ? false : filterTypeIds.contains(equipment.getTypeId())));
    }

    /**
     * 序号小的先执行
     *
     * @return
     */
    @Override
    public int getOrder() {
        return 4;
    }
}
